namespace SimpleWebServices.Web.UI.Html
{
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;

    public sealed class Names
    {
        public Names()
            : this(null)
        {
        }

        public Names(string value)
        {
            this.Items = new Collection<Name>();
            if (!string.IsNullOrEmpty(value))
            {
                string[] parts = value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string part in parts)
                {
                    this.Items.Add(part);
                }
            }
        }

        private Collection<Name> Items
        {
            get;
            set;
        }

        public static implicit operator Names(string value)
        {
            return (null == value) ? null : new Names(value);
        }

        public static implicit operator string(Names names)
        {
            return (null == names) ? null : names.ToString();
        }

        public override string ToString()
        {
            StringBuilder buffer = new StringBuilder();
            foreach (Name name in this.Items.Where(x => null != x))
            {
                if (0 != buffer.Length)
                {
                    buffer.Append(' ');
                }

                buffer.Append(name);
            }

            return buffer.ToString();
        }
    }
}